import org.apache.spark.ml.fpm.{FPGrowth, FPGrowthModel}
import org.apache.spark.sql.{DataFrame, SparkSession}
import org.apache.spark.{SparkConf, SparkContext}


object FPtest{

  def main(args: Array[String]): Unit = {

    val sc = new SparkContext(new SparkConf().setMaster("local[*]").setAppName(""))

    val spark: SparkSession = SparkSession
      .builder
      .appName(s"${this.getClass.getSimpleName}")
      .getOrCreate()
    import spark.implicits._

    sc.setLogLevel("error")

    val dataset: DataFrame = sc.textFile("in/Groceries.txt")
      .map(s=>{s.substring(1,s.length-1)
        .split("\" \"")})
      .toDF("items")

    val fpgrowth: FPGrowth = new FPGrowth().setItemsCol("items").setMinSupport(0.015).setMinConfidence(0.25)

    val model: FPGrowthModel = fpgrowth.fit(dataset)

    val test_set: DataFrame = spark.createDataset(Seq(
      "\"cream cheese\" \"shopping bags\" \"white bread\"",
      "\"beef\" \"whole milk\"",
      "\"pickled vegetables\"")
    ).map(s=>{s.substring(1,s.length-1).split("\" \"")}).toDF("items")

    println("朱秉柯 183080106")
//    model.freqItemsets.show()
    model.transform(test_set).show()

    spark.stop()
  }
}
